<?php
class Page{
	private $_data;
	//Costruttore
	//lang il country code della lingua
	//strings le etichette delle stringhe
	function __construct( $lang, $labels, $contents ){
		//Setta la lingua
		$this->_data['lang'] = $lang;
		$this->_data['labels'] = $labels;	
		$this->_data['contents'] = $contents;
		//Carica le stringhe
		if( count( $this->_data['labels'] ) ){
			$this->_loadStrings( );
		}
	}
	//carica tutte le stringhe tradotte
	private function _loadStrings( ){
		global $_CONF;
		//Logger
		$_logger = new Logger( );
		//Carica tutte le traduzioni per la lingua selezionata
		$_table = $_CONF['db']['tables']['strings'];
		foreach( $this->_data['labels'] as $key=>$value ){
			$this->_data['labels'][$key] = "'{$value}'";
		}
		$_stringsSet = implode( ',', $this->_data['labels'] );
		$_sql_select_strings = 
		"
			SELECT label, {$this->_data['lang']} as tr_string FROM {$_table}
				WHERE label IN( {$_stringsSet} )
		";
		$_db = new MysqlDB( );
		if( !$_db->connectionReady( ) ){
			$_logger->log( $_CONF['log_levels']['FATAL_ERROR'] , 'Error while connecting to DB', 0, 'Page class' );
			return false;
		}
		$_rs = $_db->monitoredQuery( $_sql_select_strings );
		if( !$_rs ){
			$_logger->log( $_CONF['log_levels']['FATAL_ERROR'] , 'Error while extracting system strings translations from DB', 0, 'Page class' );
			return false;
		}
		$this->_data['strings'] = array( );
		if( $_rs->num_rows ){
			while( $_tr_string = mysql_fetch_assoc( $_rs ) ){
				$this->_data['strings'][$_tr_string['label']] = $_tr_string['tr_string'];
			}
		}
		//Segna le label non trovate
		//in modo che verranno stampati a video dei messaggi di warning
		foreach( $this->_data['labels'] as $value ){
			$_value = str_replace( "'", '', $value );
			if( !isset( $this->_data['strings'][$value] ) or $this->_data['strings'][$value] === null ){
				$this->_data['strings'][$_value] = "NO_TRANSLATION_{$_value}";
			}
		}

	}
	//Restituisce i dati relativi alla pagina
	public function get( ){
		return $this->_data;
	}
	//Explain di debug
	public function explain( ){
		print_r( $this->_data );
	}
}
?>